package com.niiwoo.civet.trade.service.local.credit;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.niiwoo.civet.trade.dao.criteria.InvestigationPersonCriteria;
import com.niiwoo.civet.trade.dao.entity.credit.CreditInvestigationPerson;
import com.niiwoo.civet.trade.dao.mapper.credit.CreditInvestigationPersonMapperExt;
import com.niiwoo.civet.trade.dto.response.credit.InvestigationProjectDTO;
import com.niiwoo.tripod.base.utils.DateUtils;
import com.niiwoo.tripod.provider.dto.response.PageResponseDTO;

/**
 * @ClassName: CreditInvestigationPersonService
 * @Description: 尽调相关本地服务
 *
 * @author: kevin.jia
 * @version 
 * @date: 2017年12月14日 下午3:19:04
 */
@Service("creditInvestigationPersonService")
public class CreditInvestigationPersonService {

	@Autowired
	private CreditInvestigationPersonMapperExt creditInvestigationPersonMapperExt;
	
	/**
	 * 查询我的尽调项目列表
	 * 
	 * @param userId
	 * @return
	 */
	public List<CreditInvestigationPerson> getCreditInvestigationPersonList(String userId, Integer pageNumber, Integer pageSize) {
		InvestigationPersonCriteria userCriteria = new InvestigationPersonCriteria();
		userCriteria.setInvestigationUserId(userId);
		userCriteria.setOffset((pageNumber - 1) * pageSize);
		userCriteria.setRows(pageSize);
		List<CreditInvestigationPerson> creditInvestigationPersonList = creditInvestigationPersonMapperExt
				.selectByCriteria(userCriteria);
		
		return creditInvestigationPersonList;
	}
	
	/**
	 * 查询我的尽调项目列表
	 * 
	 * @param userId	用户ID
	 * @param pageNumber	页码
	 * @param pageSize	单页显示最大条目
	 *
	 * @return
	 * 			信用借项目列表
	 */
	public PageResponseDTO<InvestigationProjectDTO> getInvestigationProjectPage(String userId, Integer pageNumber, Integer pageSize) {
		
		PageResponseDTO<InvestigationProjectDTO> pageResponseDTO = new PageResponseDTO<InvestigationProjectDTO>();
		
		InvestigationPersonCriteria investigationUserCriteria = new InvestigationPersonCriteria();
		investigationUserCriteria.setInvestigationUserId(userId);
		Integer totalCount = creditInvestigationPersonMapperExt.countInvestigationProjectByCriteria(investigationUserCriteria);
		if (null != totalCount && totalCount > 0) {
			// 增加分页参数
			investigationUserCriteria.setOffset((pageNumber - 1) * pageSize);
			investigationUserCriteria.setRows(pageSize);
			List<InvestigationProjectDTO> investigationProjectDTOList = creditInvestigationPersonMapperExt.selectInvestigationProjectByCriteria(investigationUserCriteria);
			
			pageResponseDTO.setItems(investigationProjectDTOList);
			pageResponseDTO.measureTotalPage(totalCount, pageSize);
		} else {
			pageResponseDTO.measureTotalPage(0, pageSize);
			pageResponseDTO.setItems(Lists.newArrayList());
		}
		return pageResponseDTO;
	}
	
	/**
	 * 统计历史累计尽调成功笔数
	 * 
	 * @param userId
	 * @return
	 */
	public Integer getInvestigationTotalNumber(String userId) {
		return creditInvestigationPersonMapperExt.countEffectiveByUserId(userId);
	}

	/**
	 * 获取累计尽调收益金额
	 * 
	 * @param userId
	 * @return
	 */
	public BigDecimal getAccruedIncomeAmount(String userId) {
		return creditInvestigationPersonMapperExt.countIncomeAmountByUserId(userId, null);
	}
	
	/**
	 * 获取最近三个累计尽调收益
	 * 
	 * @param userId
	 * @return
	 */
	public BigDecimal getLatest3AccruedIncomeAmount(String userId) {
		// 获取三个月前日期
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.MONTH, -3);
		String beginTime = DateUtils.format(ca.getTime(), "yyyy-MM");
		return creditInvestigationPersonMapperExt.countIncomeAmountByUserId(userId, beginTime);
	}
}